<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create new session id</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.session-create-id" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_create_id</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.1.0)</p><p class="refpurpose"><span class="refname">session_create_id</span> &mdash; <span class="dc-title">Create new session id</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.session-create-id-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>session_create_id</strong></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>session_create_id()</strong></span> is used to create new
   session id for the current session. It returns collision free
   session id.
  </p>
  <p class="para">
   If session is not active, collision check is omitted.
  </p>
  <p class="para">
   Session ID is created according to php.ini settings.
  </p>
  <p class="para">
   It is important to use the same user ID of your web server for GC
   task script. Otherwise, you may have permission problems especially
   with files save handler.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.session-create-id-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">prefix</code></dt>

      <dd>

       <p class="para">
        If <code class="parameter">prefix</code> is specified, new session id
        is prefixed by <code class="parameter">prefix</code>. Not all
        characters are allowed within the session id.  Characters in
        the range <em>a-z A-Z 0-9 , (comma) and -
        (minus)</em> are allowed.
       </p> 
      </dd>

     
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-create-id-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>session_create_id()</strong></span> returns new collision free
   session id for the current session. If it is used without active
   session, it omits collision check.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.session-create-id-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5831">
    <p><strong>Example #1 <span class="function"><strong>session_create_id()</strong></span> example with <span class="function"><a href="session_regenerate_id.html" class="function">session_regenerate_id()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;My&nbsp;session&nbsp;start&nbsp;function&nbsp;support&nbsp;timestamp&nbsp;management<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_session_start</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Do&nbsp;not&nbsp;allow&nbsp;to&nbsp;use&nbsp;too&nbsp;old&nbsp;session&nbsp;ID<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!empty(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'deleted_time'</span><span style="color: #007700">])&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'deleted_time'</span><span style="color: #007700">]&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()&nbsp;-&nbsp;</span><span style="color: #0000BB">180</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_destroy</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;My&nbsp;session&nbsp;regenerate&nbsp;id&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_session_regenerate_id</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Call&nbsp;session_create_id()&nbsp;while&nbsp;session&nbsp;is&nbsp;active&nbsp;to&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;make&nbsp;sure&nbsp;collision&nbsp;free.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">session_status</span><span style="color: #007700">()&nbsp;!=&nbsp;</span><span style="color: #0000BB">PHP_SESSION_ACTIVE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;WARNING:&nbsp;Never&nbsp;use&nbsp;confidential&nbsp;strings&nbsp;for&nbsp;prefix!<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$newid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">session_create_id</span><span style="color: #007700">(</span><span style="color: #DD0000">'myprefix-'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Set&nbsp;deleted&nbsp;timestamp.&nbsp;Session&nbsp;data&nbsp;must&nbsp;not&nbsp;be&nbsp;deleted&nbsp;immediately&nbsp;for&nbsp;reasons.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'deleted_time'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Finish&nbsp;session<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_commit</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Make&nbsp;sure&nbsp;to&nbsp;accept&nbsp;user&nbsp;defined&nbsp;session&nbsp;ID<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;NOTE:&nbsp;You&nbsp;must&nbsp;enable&nbsp;use_strict_mode&nbsp;for&nbsp;normal&nbsp;operations.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.use_strict_mode'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Set&nbsp;new&nbsp;custom&nbsp;session&nbsp;ID<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_id</span><span style="color: #007700">(</span><span style="color: #0000BB">$newid</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Start&nbsp;with&nbsp;custom&nbsp;session&nbsp;ID<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Make&nbsp;sure&nbsp;use_strict_mode&nbsp;is&nbsp;enabled.<br />//&nbsp;use_strict_mode&nbsp;is&nbsp;mandatory&nbsp;for&nbsp;security&nbsp;reasons.<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.use_strict_mode'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">my_session_start</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Session&nbsp;ID&nbsp;must&nbsp;be&nbsp;regenerated&nbsp;when<br />//&nbsp;&nbsp;-&nbsp;User&nbsp;logged&nbsp;in<br />//&nbsp;&nbsp;-&nbsp;User&nbsp;logged&nbsp;out<br />//&nbsp;&nbsp;-&nbsp;Certain&nbsp;period&nbsp;has&nbsp;passed<br /></span><span style="color: #0000BB">my_session_regenerate_id</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Write&nbsp;useful&nbsp;codes<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-create-id-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="session_regenerate_id.html" class="function" rel="rdfs-seeAlso">session_regenerate_id()</a> - 使用新生成的会话 ID 更新现有会话 ID</span></li>
    <li class="member"><span class="function"><a href="session_start.html" class="function" rel="rdfs-seeAlso">session_start()</a> - 启动新会话或者重用现有会话</span></li>
    <li class="member"><a href="session.configuration.html#ini.session.use-strict-mode" class="link">session.use_strict_mode</a></li>
    <li class="member"><span class="methodname"><a href="sessionhandler.create_sid.html" class="methodname" rel="rdfs-seeAlso">SessionHandler::create_sid()</a> - Return a new session ID</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>